class RequirementController < ApplicationController
  before_filter :protect
  before_filter :protect_requirement, :only => [:edit, :destroy, :choose_order, :choose_solver, :delete_order]
  before_filter :check_role, :only => [:new,:edit,:destroy]
  layout 'user'
  
  def index
    @title = "我的需求"
    @requirements = @login_user.requirements.paginate(:page => params[:page],
                                                :per_page => PERPAGE,
                                                :order => "updated_at DESC")
  end
  
  def new
    @title = "发布需求"
    if param_posted?(:requirement)
      @requirement = Requirement.new(params[:requirement])
      @requirement.user = @login_user
      if @login_user.is_vip == NO
        deal_notvip_requirement(@login_user,@requirement)
      else
        deal_vip_requirement(@login_user,@requirement)
      end
    end
  end
  
  def edit
    @title = "编辑#{@requirement.title}"
    if param_posted?(:requirement)
      if @requirement.update_attributes(params[:requirement])
        flash[:notice] = "更新成功"
        redirect_to :action => :show, :id => params[:id]
      end
    end
  end
  
  def show
    @requirement = Requirement.find_by_id(params[:id])
    @title = @requirement.title
    @requirement.click_times += 1
    @requirement.save
    
    create_comment                                               
    render :layout => false                                               
  end
  
  def create_comment
    if param_posted?(:comment)
      if @login_user.role != DESIGNER
        redirect_to :action => "something_wrong"
        return
      end
      @comment = Comment.new(params[:comment])
      @comment.user = @login_user
      @comment.requirement = @requirement
      if @comment.duplicate? or (@comment.save && @requirement.comments << @comment)
        @comment = nil
      end
#      redirect_to :action => :show, :id => @requirement
    end
  end
  
  def reply_comment
    @requirement = Requirement.find(params[:id])
    if param_posted?(:comment)
      @comment = Comment.new(params[:comment])
      @comment.user = @login_user
      @comment.requirement = @requirement
      @comment.reply_id=params[:reply_id]
      if @comment.duplicate? or (@comment.save && @requirement.comments << @comment)
        flash[:notice] = "评论成功"
      else
        flash[:notice] = "评论失败"
      end
      redirect_to :action => :show, :id => @requirement
    end
  end
  
  def destroy
    @login_user.requirements.destroy(@requirement)
    redirect_to :action => :index
  end
  
  def destroy_comment
    @comment = Comment.find(params[:id])
    if @comment.authorized?(@login_user)
      @comment.destroy
      flash[:notice] = "成功删除#{@login_user.username}的评论"
    else
      flash[:notice] = "删除评论失败"
    end
    redirect_to :controller => :requirement, :action => :show, :id => @requirement
  end
  
  def choose_order
    @user = User.find_by_username(params[:username])
    RequirementOrder.confirm_user(@user,@requirement)
    render :partial => 'choose_order'
  end
  
  def delete_order
    @user = User.find_by_username(params[:username])
    RequirementOrder.break_user(@user,@requirement)
    render :partial => 'delete_order'
  end
  
  def choose_solver
    @user = User.find_by_username(params[:username])
    @requirement.solver = @user
    @requirement.status = "solved"
    @requirement.save!
    render :partial => 'choose_solver'
  end
  
  def download
    requirement = Requirement.find_by_id(params[:id])
    if requirement
      send_file requirement.attach.path, :filename => requirement.attach_file_name    #:type=>"application/zip"  
    end
     
  end
  
  private
  
  def check_role
    redirect_to :action => "something_wrong" unless @login_user.role == DEMANDER
  end
  
  def protect_requirement
    @requirement = Requirement.find(params[:id])
    unless @requirement.user == @login_user
      flash[:notice] = "这不是您的需求"
      redirect_to :action => :index
    end
  end
  
  def deal_notvip_requirement(user,requirement)
    success = false
    if user.requirements
      if user.requirements.length >= MAX_REQUIREMENT
        flash[:notice] = "您还不是vip会员，只能创建两条需求"
      else
        success = deal_requirement(user,requirement)
      end
    else
      success = deal_requirement(user,requirement)
    end
    success
  end
  
  def deal_vip_requirement(user,requirement)
    deal_requirement(user,requirement)
  end
  
  def deal_requirement(user,requirement)
    if requirement.duplicate? or (requirement.save && user.requirements << requirement)
       flash[:notice] = "创建需求成功"
       redirect_to :action => :index
       return true
    end
    return false
  end
  
end
